package xyz.yuzegod.megawalls.classes.normal.cow;

import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class SecondSkill extends Skill
{
    public SecondSkill(final Classes classes) {
        super("奶桶屏障", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 0.25;
            }
            case 2: {
                return 0.375;
            }
            case 3: {
                return 0.5;
            }
            default: {
                return 0.25;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7当你血量低于20的时候,");
            lore.add("   §7你头顶会不断掉出奶桶,持续20秒");
            lore.add("   §7接下来4次任何来源的伤害都将被减少§a" + StringUtils.percent(this.getAttribute(level)));
            lore.add("   §7每次减伤你额外恢复2血量。");
            return lore;
        }
        lore.add(" §8▪ §7当你血量低于20的时候,");
        lore.add("   §7你头顶会不断掉出奶桶,持续20秒");
        lore.add("   §7接下来4次任何来源的伤害都将被减少§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜ §a" + StringUtils.percent(this.getAttribute(level)));
        lore.add("   §7每次减伤你额外恢复2血量。");
        lore.add(" ");
        lore.add("§7冷却时间:§a40秒");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill2Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill2Level();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        if (Cow.skill2Cooldown.getOrDefault(gamePlayer, 0) > 0) {
            return false;
        }
        final Player player = gamePlayer.getPlayer();
        new BukkitRunnable() {
            private int ticks = 0;
            private List<Item> items = new ArrayList<Item>();
            
            public void run() {
                if (!player.isOnline() || this.ticks >= 400) {
                    for (final Item item : this.items) {
                        item.remove();
                    }
                    this.cancel();
                    return;
                }
                final Item item2 = player.getWorld().dropItem(player.getLocation().add(0.0, 2.0, 0.0), new ItemStack(Material.MILK_BUCKET));
                item2.setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)MegaWalls.getFixedMetadataValue());
                final Vector vector = new Vector((MegaWalls.getRandom().nextDouble() - 0.5) / 1.7, 0.35, (MegaWalls.getRandom().nextDouble() - 0.5) / 1.7);
                item2.setVelocity(vector);
                this.items.add(item2);
                this.ticks += 5;
            }
        }.runTaskTimer((Plugin)MegaWalls.getInstance(), 0L, 5L);
        Cow.skill2Cooldown.put(gamePlayer, 40);
        Cow.skill2Damage.put(gamePlayer, 0);
        return true;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.getClasses().getNameColor() + "§l" + this.getName() + " " + ((Cow.skill2Cooldown.getOrDefault(gamePlayer, 0) == 0) ? "§a§l✓" : ("§c§l" + Cow.skill2Cooldown.get(gamePlayer) + "秒"));
    }
}
